/* 
 * @(#)ForumAdminAction.java    Created on Apr 19, 2013
 * Copyright (c) 2013 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.leruan.aasimp.action.forum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.bean.UsedCarSaleMainBean;
import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.entity.Board;
import com.leruan.aasimp.entity.Forumuser;
import com.leruan.aasimp.entity.Mainpost;
import com.leruan.aasimp.entity.Reply;
import com.leruan.aasimp.entitydto.MainPostDto;
import com.leruan.aasimp.entitydto.ReplyDto;
import com.leruan.aasimp.entitydto.SensitiveDto;
import com.leruan.aasimp.service.AdminService;
import com.leruan.aasimp.service.BoardService;
import com.leruan.aasimp.service.ForumUserService;
import com.leruan.aasimp.service.MainPostService;
import com.leruan.aasimp.service.ReplyService;
import com.leruan.aasimp.util.StringUtil;
import com.leruan.aasimp.util.Validators;
import com.opensymphony.xwork2.ActionSupport;

public class ForumAdminAction extends ActionSupport {

    private static final long serialVersionUID = -843607943822564352L;
    @Resource
    private AdminService adminService;
    @Resource
    private BoardService boardService;
    @Resource
    private ForumUserService forumUserService;
    @Resource
    private MainPostService mainPostService;
    @Resource
    private ReplyService replyService;
    private String actionErorr;
    private String actionMessage;
    private String username;
    private Long state;

    public String loginAdmin() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Forumuser user = (Forumuser) session.getAttribute("user");
        if (null == user) {
            return "login";
        }
        else {
            if (user.getUserType() == 1) {
                return "success";
            }
            return "login";
        }
    }

    public String forumEdit() {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Board> boards = boardService.getBoard();

        request.setAttribute("boards", boards);
        return "success";
    }

    public String forumEditDone() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String boardId = request.getParameter("boardId");
        String boardName = request.getParameter("boardName");
        String username = request.getParameter("username");
        Long isshow = Long.parseLong(request.getParameter("isshow"));
        Board board = boardService.getBoardById(boardId);
        if (!"".equals(username) || null != username) {
            Forumuser boardUser = forumUserService.getForumUserByUsername(username);
            if (null == boardUser) {
                addActionError("您选择的版主不存在");
            }
            else {
                board.setBoardUserId(boardUser.getUserId());
            }
        }
        board.setBoardId(boardId);
        board.setBoardName(boardName);
        board.setIsshow(isshow);
        validateBoard(board);
        List<Board> boards = boardService.getBoard();
        request.setAttribute("boards", boards);
        if (hasErrors()) {
            return "success";
        }
        boardService.modifyBoardHits(board);
        addActionMessage("板块信息编辑成功");
        return "success";
    }

    public String addForum() {
        return "success";
    }

    public String addForumDone() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String boardId = StringUtil.getUUID();
        String boardName = request.getParameter("boardName");
        String username = request.getParameter("username");
        Long isshow = Long.parseLong(request.getParameter("isshow"));
        Board board = null;
        if (Validators.isEmpty(boardName)) {
            addActionError("请为新板块取个名字吧");
        }
        if (!"".equals(username) || null != username) {
            Forumuser boardUser = forumUserService.getForumUserByUsername(username);
            if (null == boardUser) {
                addActionError("您选择的版主不存在");
            }
            else {
                board = new Board(boardId, boardName, boardUser.getUserId(), 0l, 0l, isshow);
            }
        }
        if (hasErrors()) {
            return "success";
        }
        boardService.addBoard(board);
        addActionMessage("添加板块成功");
        return "success";
    }

    public String sensitive() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        List<Board> boards = boardService.getBoard();
        Map<String, String> map = new HashMap<String, String>();
        Long total = 0l;
        int pageNo = 0;
        session.setAttribute("pageNoSale", pageNo);
        session.setAttribute("pageNoCenterSale", 3);
        session.setAttribute("totalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
        session.setAttribute("totalSale", total);
        request.setAttribute("boards", boards);
        return "success";
    }

    public String sensitiveSearch() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        String sensitiveString = request.getParameter("sensitiveString");
        String boardId = request.getParameter("boardId");
        Map<String, String> map = new HashMap<String, String>();
        map.put("boardId", boardId);
        map.put("sensitiveString", sensitiveString);
        int pageNo = 1;
        try {
            List<SensitiveDto> sensitiveList = adminService.getSensitive(boardId, sensitiveString, pageNo);
            Long total = adminService.getSensitiveTotal(map);
            List<Board> boards = boardService.getBoard();
            request.setAttribute("boards", boards);
            request.setAttribute("boardId", boardId);
            request.setAttribute("sensitiveList", sensitiveList);
            request.setAttribute("sensitiveString", sensitiveString);
            request.setAttribute("boardId", boardId);
            session.setAttribute("pageNoSale", pageNo);
            session.setAttribute("pageNoCenterSale", 3);
            session.setAttribute("totalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalSale", total);
        }
        catch (Exception e) {
            e.printStackTrace();

        }

        return "success";
    }

    public String sensitiveQuery() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        String sensitiveString = request.getParameter("sensitiveString");
        String boardId = request.getParameter("boardId");
        int pageNo = Integer.parseInt(request.getParameter("no"));
        if (null == sensitiveString) {
            sensitiveString = "";
        }
        try {
            List<SensitiveDto> sensitiveList = adminService.getSensitive(boardId, sensitiveString, pageNo);
            List<Board> boards = boardService.getBoard();
            request.setAttribute("boardId", boardId);
            request.setAttribute("sensitiveString", sensitiveString);
            request.setAttribute("sensitiveList", sensitiveList);
            request.setAttribute("boards", boards);
            session.setAttribute("pageNoSale", pageNo);
            Long total = (Long) session.getAttribute("totalNoSale");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterSale", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterSale", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterSale", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    public String batchRemove() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        List<Board> boards = boardService.getBoard();
        Map<String, String> map = new HashMap<String, String>();
        request.setAttribute("map", map);
        request.setAttribute("boards", boards);
        return "success";
    }

    public String batchRemovePost() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Map<String, String> map = new HashMap<String, String>();
        List<MainPostDto> mainPostListDto = new ArrayList<MainPostDto>();
        List<ReplyDto> replyListDto = new ArrayList<ReplyDto>();
        List<Board> boards = boardService.getBoard();
        String userId = null;
        String boardId = request.getParameter("boardId");
        String username = request.getParameter("username");
        String postTitle = request.getParameter("postTitle");
        String postDateStart = request.getParameter("postDateStart");
        String postDateEnd = request.getParameter("postDateEnd");
        String postOrReplyStr = request.getParameter("postOrReply");
        int postOrReply;
        if (Validators.isEmpty(postOrReplyStr)) {
            postOrReply = 0;
        }
        else {
            postOrReply = Integer.parseInt(postOrReplyStr);
        }
        if (!Validators.isEmpty(username)) {

            Forumuser forumUser = forumUserService.getForumUserByUsername(username);
            if (null != forumUser) {
                userId = forumUser.getUserId();
            }
            else {
                addActionError("不存在此用户名，因此结果中没有对用户名进行筛选");
            }
        }
        map.put("boardId", boardId);
        map.put("username", username);
        map.put("userId", userId);
        map.put("postTitle", postTitle);
        map.put("postDateStart", postDateStart);
        map.put("postDateEnd", postDateEnd);
        int pageNo = 1;
        Long total = null;
        if (0 == postOrReply) {
            mainPostListDto = mainPostService.getMainPostsByBoardId(boardId, map, pageNo);
            total = mainPostService.getMainPostTotal(boardId, map);
            request.setAttribute("postOrReply", 0);
            request.setAttribute("mainPostListDto", mainPostListDto);
        }
        else if (1 == postOrReply) {
            replyListDto = replyService.getReply(map, pageNo);
            total = replyService.getReplyTotal(map);
            request.setAttribute("postOrReply", 1);
            request.setAttribute("replyListDto", replyListDto);
        }
        try {
            request.setAttribute("boards", boards);
            request.setAttribute("map", map);
            request.setAttribute("postOrReply", postOrReply);
            session.setAttribute("pageNoSale", pageNo);
            session.setAttribute("pageNoCenterSale", 3);
            session.setAttribute("totalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalSale", total);
        }
        catch (Exception e) {
            e.printStackTrace();

        }
        return "success";
    }

    public String batchRemovePostQuery() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Map<String, String> map = (Map<String, String>) request.getAttribute("map");
        List<MainPostDto> mainPostListDto = new ArrayList<MainPostDto>();
        List<ReplyDto> replyListDto = new ArrayList<ReplyDto>();
        List<Board> boards = boardService.getBoard();
        String userId = null;
        String boardId = map.get("boardId");
        String username = map.get("username");
        int postOrReply = Integer.parseInt(map.get("postOrReply"));
        if (Validators.isEmpty(username)) {
            Forumuser forumUser = forumUserService.getForumUserByUsername(username);
            if (null != forumUser) {
                userId = forumUser.getUserId();
            }
            else {
                addActionError("不存在此用户名，因此结果中没有对用户名进行筛选");
            }
        }
        int pageNo = Integer.parseInt(request.getParameter("no"));

        if (0 == postOrReply) {
            mainPostListDto = mainPostService.getMainPostsByBoardId(boardId, map, pageNo);
            request.setAttribute("postOrReply", 0);
            request.setAttribute("mainPostListDto", mainPostListDto);
        }
        else if (1 == postOrReply) {
            replyListDto = replyService.getReply(map, pageNo);
            request.setAttribute("postOrReply", 1);
            request.setAttribute("replyListDto", replyListDto);
        }
        try {
            request.setAttribute("boards", boards);
            request.setAttribute("map", map);
            request.setAttribute("postOrReply", postOrReply);
            session.setAttribute("pageNoSale", pageNo);
            Long total = (Long) session.getAttribute("totalNoSale");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterSale", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterSale", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterSale", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return "success";
    }

    private void validateBoard(Board board) {
        if (Validators.isEmpty(board.getBoardId())) {
            addActionError("请选择一个板块");
        }
        if (Validators.isEmpty(board.getBoardName())) {
            addActionError("请填写新的板块名称");
        }
        // if (Validators.isEmpty(board.getBoardUserId())) {
        // addActionError("请输入版主编号");
        // }
    }

    public String editUser() {
        return "success";
    }

    public String serchUserForEdit() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        List<Forumuser> forumUser = new ArrayList<Forumuser>();
        if (null == state) {
            state = 0l;
        }
        // username = request.getParameter("username");
        // String stateStr = request.getParameter("state");
        // if (Validators.isEmpty(stateStr)) {
        // state = 0l;
        // }
        // else {
        // state = Long.parseLong(stateStr);
        // }
        int pageNo = 1;
        try {
            forumUser = forumUserService.getForumUser(username, state, pageNo);
            Long total = forumUserService.getForumUserTotal(username, state);
            request.setAttribute("forumUser", forumUser);
            request.setAttribute("username", username);
            request.setAttribute("state", state);
            session.setAttribute("pageNoSale", pageNo);
            session.setAttribute("pageNoCenterSale", 3);
            session.setAttribute("totalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalSale", total);
        }
        catch (Exception e) {
            e.printStackTrace();

        }
        return "success";
    }

    public String editUserQuery() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Map<String, String> map = new HashMap<String, String>();
        List<Forumuser> forumUser = new ArrayList<Forumuser>();
        int pageNo = Integer.parseInt(request.getParameter("no"));
        String username = request.getParameter("username");
        Long state = Long.parseLong(request.getParameter("state"));
        List<UsedCarSaleMainBean> usedCarList;
        try {
            forumUser = forumUserService.getForumUser(username, state, pageNo);
            request.setAttribute("forumUser", forumUser);
            request.setAttribute("username", username);
            request.setAttribute("state", state);
            session.setAttribute("pageNoSale", pageNo);
            Long total = (Long) session.getAttribute("totalNoSale");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterSale", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterSale", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterSale", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    public String deleteUser() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        String userId = request.getParameter("userId");
        String username = request.getParameter("username");
        String state = request.getParameter("state");
        if (Validators.isEmpty(userId)) {
            actionErorr = "请选择要删除的用户";
            session.removeAttribute("actionErorr");
            session.setAttribute("actionErorr", actionErorr);
            request.setAttribute("username", username);
            request.setAttribute("state", state);
            return serchUserForEdit();
        }
        else {
            Forumuser forumuser = forumUserService.getBoardUserById(userId);
            if (null == forumuser) {
                actionErorr = "你要删除的用户不存在";
                session.removeAttribute("actionErorr");
                session.setAttribute("actionErorr", actionErorr);
                request.setAttribute("username", username);
                request.setAttribute("state", state);
            }
            else {
                boardService.modifyBoardUser(forumuser.getUserId(), null);
                List<Mainpost> mainposts = mainPostService.getMainPostsByUserId(forumuser.getUserId());
                for (Mainpost mainpost : mainposts) {
                    mainpost.setIddelete(1l);
                    mainPostService.modifyMainPost(mainpost);
                }
                List<Reply> replys = replyService.getReplyByUserId(forumuser.getUserId());
                for (Reply reply : replys) {
                    reply.setIsshow(0l);
                    replyService.modifyReply(reply);
                }
                forumUserService.removeUser(forumuser);
                request.setAttribute("username", username);
                request.setAttribute("state", state);
            }
        }
        return serchUserForEdit();
    }
	public String modifyUser(){
		 HttpServletRequest request = ServletActionContext.getRequest();
		 String userId = request.getParameter("m_userId");
		 String username = request.getParameter("m_username");
		 String nickname = request.getParameter("m_nickname");
		 Long state = Long.parseLong( request.getParameter("m_state"));
		 Long grade = Long.parseLong( request.getParameter("m_grade"));
		 Long score = Long.parseLong(request.getParameter("m_score"));
		 Long userType =Long.parseLong(request.getParameter("m_userType"));
		 Forumuser user = forumUserService.getForumUserByUsername(username);
		 user.setNickname(nickname);
		 user.setGrade(grade);
		 user.setScore(score);
		 user.setUserType(userType);
		 forumUserService.modifyForunUser(user);
		 request.setAttribute("username",username );
		 request.setAttribute("username",state );
		return "success";
	}
    public AdminService getAdminService() {
        return adminService;
    }

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public BoardService getBoardService() {
        return boardService;
    }

    public void setBoardService(BoardService boardService) {
        this.boardService = boardService;
    }

    public ForumUserService getForumUserService() {
        return forumUserService;
    }

    public void setForumUserService(ForumUserService forumUserService) {
        this.forumUserService = forumUserService;
    }

    public MainPostService getMainPostService() {
        return mainPostService;
    }

    public void setMainPostService(MainPostService mainPostService) {
        this.mainPostService = mainPostService;
    }

    public ReplyService getReplyService() {
        return replyService;
    }

    public void setReplyService(ReplyService replyService) {
        this.replyService = replyService;
    }

    public String getActionErorr() {
        return actionErorr;
    }

    public void setActionErorr(String actionErorr) {
        this.actionErorr = actionErorr;
    }

    public String getActionMessage() {
        return actionMessage;
    }

    public void setActionMessage(String actionMessage) {
        this.actionMessage = actionMessage;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Long getState() {
        return state;
    }

    public void setState(Long state) {
        this.state = state;
    }

}
